import { useEffect } from 'react'
import useGetUserInfo from './useGetUserInfo'
import { useLocation, useNavigate } from 'react-router-dom'
import {
  isLoginOrRegister,
  isNoNeedUserInfo,
  MANAGE_INDEX_PATHNAME,
  LOGIN_PATHNAME,
} from '../router/index'
import path from 'path'
function useNavPage(waitingUserDate: boolean) {
  const { username } = useGetUserInfo()
  const { pathname } = useLocation()
  const nav = useNavigate()

  useEffect(() => {
    if (waitingUserDate) return
    //TODO 已经登陆了
    if (username) {
      isLoginOrRegister(pathname) && nav(MANAGE_INDEX_PATHNAME)
      return
    }
    //TODO 未登录
    if (isNoNeedUserInfo(pathname)) {
      return
    } else {
      nav(LOGIN_PATHNAME)
    }
  }, [username, pathname, waitingUserDate])
}

export default useNavPage
